<?php

namespace App\Core\Utils\TwHelper;

use App\Models\Club\MemberModel;
use App\Core\Utils\TwHelper\Random;

class CommFunction
{

    /**
     * 获取真实IP
     * @return ip
     */
    public static function getip()
    {
        $unknown = 'unknown';
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        /**
         * 处理多层代理的情况
         * 或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
         */
        if (false !== strpos($ip, ','))
            $ip = reset(explode(',', $ip));
        return $ip;
    }

    /**
     * 生成一个社区的username
     */
    public static function createBbsUserName($userName = '')
    {
        if ($userName) {
            if (MemberModel::getUserByUserName($userName)) {
                $userName = self::createBbsNameById();
                if (MemberModel::getUserByUserName($userName)) {
                    return self::createBbsNameByRandom();
                } else {
                    return $userName;
                }
            } else {
                return $userName;
            }
        } else {
            $userName = self::createBbsNameById();
            if (MemberModel::getUserByUserName($userName)) {
                return self::createBbsNameByRandom();
            } else {
                return $userName;
            }
        }
    }

    public static function createBbsNameByRandom()
    {
        return '通心粉' . strtolower(Random::getRandomPwd(5, ''));
    }

    public static function createBbsNameById()
    {
        $nextInsertId = MemberModel::getLastInsertId() + 1;
        dd();
        $userName = '通心粉' . $nextInsertId;
    }

    public static function checkMobile($mobile)
    {
        $mobile = trim($mobile, '');
        $top = array(
            '121',
            '130',
            '131',
            '132',
            '133',
            '134',
            '135',
            '136',
            '137',
            '138',
            '139',
            '145',
            '147',
            '150',
            '151',
            '152',
            '153',
            '154',
            '155',
            '156',
            '157',
            '158',
            '159',
            '170',
            '171',
            '173',
            '175',
            '176',
            '177',
            '178',
            '180',
            '181',
            '182',
            '183',
            '184',
            '185',
            '186',
            '187',
            '188',
            '189'
        );
        if (!$mobile) {
            return false;
        } else if (!preg_match('/^[0-9]{11}$/', $mobile)) {
            return false;
        } else if (!in_array(substr($mobile, 0, 3), $top)) {
            return false;
        }
        return true;
    }


    /**
     * 根据ip获取到地理信息
     * 返回地理信息对象
     * eg:
     * { ["status"]=> string(1) "1"
     * ["info"]=> string(2) "OK" ["infocode"]=> string(5) "10000"
     * ["province"]=> string(9) "四川省" ["city"]=> string(9)
     * "成都市" ["adcode"]=> string(6) "510100"
     * ["rectangle"]=> string(47) "103.9017713,30.53006918;104.2544496,30.79041003" }
     */
    public static function getCityByIp($ip)
    {
        $url = "http://restapi.amap.com/v3/ip?key=8f382167000a583722a075d508b946c2&ip=" . $ip;
        return json_decode(file_get_contents($url));
    }

    /**
     * 通过高德的接口获取地理信息
     */
    public static function getCityByGaoDe()
    {
        $url = 'http://restapi.amap.com/v3/ip?key=8f382167000a583722a075d508b946c2';
        return json_decode(file_get_contents($url));
    }


    /**
     * 获取当天的凌晨时间戳
     */
    public static function getDawnTime()
    {
        return strtotime(date('Y-m-d'));
    }

    /**
     * 生成密码的key值
     *
     * @return type
     */
    public static function makeRandomKey()
    {
        $colors = array();
        for ($i = 0; $i < 6; $i++) {
            $colors[] = dechex(rand(0, 15));
        }
        return implode('', $colors);
    }

    /**
     * 生成密码
     */
    public static function makePassword($string, $key)
    {
        return md5(md5($string) . $key);
    }

    /**
     * get方法
     *
     * @param unknown $url
     * @return multitype:
     */
    public static function getCurl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        $r = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($r);
        $re = get_object_vars($result);
        return $re;
    }

    /**
     * post方法
     *
     * @param unknown $uri
     * @param unknown $data
     * @return multitype:
     */
    public static function postCurl($url, $xjson)
    {
        $jsonMenu = json_encode($xjson);
        $ch1 = curl_init($url);
        curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $xjson);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($xjson)
        ));
        $respose_data = curl_exec($ch1);
        $result = get_object_vars(json_decode($respose_data));
        return $result;
    }

}